#include "homeview.h"
#include "user/loginview.h"
#include "user/registerview.h"
#include "yingce.h"
#include <QHBoxLayout>
#include <QStackedLayout>

HomeView::HomeView(QWidget* parent) : QFrame(parent)
{
    QHBoxLayout* hl = new QHBoxLayout(this);
    hl->setContentsMargins(0, 0, 0, 0);
    hl->setSpacing(0);

    QFrame* ad = new QFrame(this);
    ad->setObjectName("ad");
    hl->addWidget(ad, 1);

    QVBoxLayout* vl = new QVBoxLayout;
    vl->setContentsMargins(20, 80, 20, 0);
    vl->setSpacing(0);
    hl->addLayout(vl);

    stacked_layout_ = new QStackedLayout();
    stacked_layout_->setContentsMargins(0, 0, 0, 0);
    vl->addLayout(stacked_layout_);
    vl->addStretch(1);

    login_view_ = new LoginView(this);
    stacked_layout_->addWidget(login_view_);
    stacked_layout_->setCurrentIndex(0);

    register_view_ = new RegisterView(this);
    stacked_layout_->addWidget(register_view_);

    connect(login_view_, &LoginView::registerRequested, this,
            [this] { stacked_layout_->setCurrentWidget(register_view_); });
    connect(login_view_, &LoginView::cancelRequested, this, [] { YingCe::instance()->quitApp(); });
    connect(login_view_, &LoginView::finished, this, &HomeView::finished);
    connect(register_view_, &RegisterView::loginRequested, this, [this](const QString& name) {
        login_view_->setName(name);
        stacked_layout_->setCurrentWidget(login_view_);
    });
    connect(register_view_, &RegisterView::cancelRequested, this,
            [] { YingCe::instance()->quitApp(); });
}

void HomeView::registerUser()
{
    stacked_layout_->setCurrentWidget(register_view_);
}
